using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 石化Buff - 使目标完全无法行动，类似眩晕但更严重
/// </summary>
public class PetrifyBuff : BuffBase
{
    private bool m_OriginalCanMove;
    private bool m_OriginalCanAttack;
    private bool m_OriginalCanCast;
    private float m_OriginalSpeed;
    private Color m_OriginalColor;
    private SpriteRenderer[] m_SpriteRenderers;

    public PetrifyBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始状���
        m_OriginalCanMove = Target.MoveComponent != null;
        m_OriginalCanAttack = Target.AttackComponent != null;
        m_OriginalCanCast = Target.SkillAttackIndicators != null;
        
        if (Target.MoveComponent != null)
        {
            m_OriginalSpeed = Target.MoveComponent.Speed;
            Target.MoveComponent.SetSpeed(0f);
            Target.MoveComponent.enabled = false;
        }
        
        if (Target.AttackComponent != null)
        {
            Target.AttackComponent.enabled = false;
        }
        
        if (Target.SkillAttackIndicators != null)
        {
            Target.SkillAttackIndicators.enabled = false;
        }
        
        // 获取所有SpriteRenderer并保存原始颜���
        m_SpriteRenderers = Target.GetComponentsInChildren<SpriteRenderer>();
        if (m_SpriteRenderers.Length > 0)
        {
            m_OriginalColor = m_SpriteRenderers[0].color;
            
            // 设置石化颜色（灰色）
            foreach (var renderer in m_SpriteRenderers)
            {
                renderer.color = Color.gray;
            }
        }
        
        // 播放石化特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("petrified", true);
            Target.Animator.speed = 0f; // 停止动画
        }
        
        Logger.Log($"{Target.name} 被石化了，完全无法行���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 石化期间不需要特殊处���
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 石化效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始状���
        if (m_OriginalCanMove && Target.MoveComponent != null)
        {
            Target.MoveComponent.SetSpeed(m_OriginalSpeed);
            Target.MoveComponent.enabled = true;
        }
        
        if (m_OriginalCanAttack && Target.AttackComponent != null)
        {
            Target.AttackComponent.enabled = true;
        }
        
        if (m_OriginalCanCast && Target.SkillAttackIndicators != null)
        {
            Target.SkillAttackIndicators.enabled = true;
        }
        
        // 恢复原始颜色
        if (m_SpriteRenderers != null)
        {
            foreach (var renderer in m_SpriteRenderers)
            {
                renderer.color = m_OriginalColor;
            }
        }
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("petrified", false);
            Target.Animator.speed = 1f; // 恢复动画速度
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Petrify"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 石化效果不能叠加，只能刷新持续时���
    }
}
